<?php
	// 1.5 //
	defined( '_JEXEC' ) or die( 'Restricted access' );
	$reg = JRegistry::getInstance(('mojo'));
	global $mainframe;
	//////////
/**
  * @version $Id: admin.php,v0.16 2007/12/16 11:55:55 visualweb Exp $ 
  * @package com_mojo
  * @copyright (c) 1guywebdesign.com 
  * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL 
  */

/**
 * is this 'abspath' necessary? 110308-dn
 */
//if (!defined('ABSPATH')) {
	//define( '_VALID_MOS', 1 );
///////////////////////////////////////////////////////
// 1.5 //
	/**
	 * quick little function to help debug 1.5 compatibility;
	 *
	 * @param string $msg unique error message
	 */
	function notCompat($msg = 'Unknown Problem.'){
		die($msg.'.  Exiting due to incompatiblilty');
	}
	
	//$baseDir = 	dirname(__FILE__).DS.'..'.DS.'..'.DS.'..';
	//define('JPATH_BASE', $baseDir );
	//notCompat('Forced Halt line 26');
	//require_once( JPATH_BASE .DS.'administrator'.DS.'includes'.DS.'defines.php' );
	//require_once( JPATH_BASE .DS.'administrator'.DS.'includes'.DS.'framework.php' );
	//require_once( JPATH_BASE .DS.'administrator'.DS.'includes'.DS.'helper.php' );
	//require_once( JPATH_BASE .DS.'administrator'.DS.'includes'.DS.'toolbar.php' );
	//notCompat('Forced Halt line 31');

	/**
	 *  CREATE THE APPLICATION
	 *  NOTE :
	 */
	//$mainframe =& JFactory::getApplication('administrator');

	/**
 	* INITIALISE THE APPLICATION
 	*
 	* NOTE :
	*/
	//$mainframe->initialise(array(
	//	'language' => $mainframe->getUserState( "application.lang", 'lang' )
	//));
	
	//JPluginHelper::importPlugin('system');	
/////////////////////////////////////////////////////////////////////////
/*
	// ref joomla globals needed to keep mojo on the same page.
	include_once( '../../../globals.php' );
	require_once( '../../../configuration.php' );
	require_once( '../../../includes/joomla.php' );

	global $mosConfig_absolute_path, $mosConfig_live_site, $database;

	$mainframe = new mosMainFrame( $database, isset($option), '.' );
*/
////////////////////////////////////////////////////////////////////////////////
	//global $mosConfig_absolute_path, $mosConfig_live_site, $database;
	$database = JFactory::getDBO(); //1.5		
	
	//JDEBUG ? $_MOJO_PROFILER->mark('afterMoJoGlobals') : null;
	//$mainframe->triggerEvent('onAfterInitialise');
	$my = JFactory::getUser();
	//notCompat('Accessing as guest');
	//$session  =& JFactory::getSession();
	//var_dump($session);
	//$instance =& $session->get('user');
	//var_dump($instance);
	//$mainframe->initSession();
	
	//$my = $mainframe->getUser();
	$reg->setValue('users.my',$my);	
	$user=null;
	//var_dump($my);
	if ($my->id) {
		$query = "SELECT *"
		. "\n FROM #__users"
		. "\n WHERE id = ". intval( $my->id )
		;
		$database->setQuery( $query );
		$database->loadObject($user);
	} else {
		notCompat('Accessing as guest');
		/*  //needs 1.5
		session_name( md5( $mosConfig_live_site ) );
		session_start();
		$my 			= new mosUser( $database );		
		$my->id 		= mosGetParam( $_SESSION, 'session_user_id', '' );
		$my->username 	= mosGetParam( $_SESSION, 'session_username', '' );
		$my->usertype 	= mosGetParam( $_SESSION, 'session_usertype', '' );
		$my->gid 		= mosGetParam( $_SESSION, 'session_gid', '' );
		$my->params		= mosGetParam( $_SESSION, 'session_user_params', '' );
		$session_id 	= mosGetParam( $_SESSION, 'session_id', '' );
		*/
	}
	$database->setQuery("SELECT id FROM #__menu WHERE link='index.php?option=com_mojo' AND published='1'");
    $Itemid = $database->loadResult();
    //JDEBUG ? $_MOJO_PROFILER->mark('afterMoJoUserLoad') : null;
	
	//global $mosConfig_absolute_path, $mosConfig_dbprefix, $mosConfig_db, $mosConfig_user, $mosConfig_password, $mosConfig_host;

// Get globals from joomla			 
	//require_once($mosConfig_absolute_path.'/components/com_mojo/wp-config.php');
	//<<EOF //
		/////1.5/////
		$_CONFIG = JFactory::getConfig();	
		define('DB_NAME', $_CONFIG->getValue('db'));						 
		define('DB_USER', $_CONFIG->getValue('user'));					 
		define('DB_PASSWORD', $_CONFIG->getValue('password'));	 
		define('DB_HOST', $_CONFIG->getValue('host'));
		define('DB_WPTABLEPREFIX',$_CONFIG->getValue('dbprefix').'wp_');
		$reg->setValue('config.mojo_db_prefix',$_CONFIG->getValue('dbprefix').'wp_');
		// this line reserved for std db prefix definition; shouldn't need it
		define ('WPLANG', '');
		define('ABSPATH',JPATH_SITE.DS.'components'.DS.'com_mojo'.DS);
		$reg->setValue('config.abspath',JPATH_SITE.DS.'components'.DS.'com_mojo');//1.5
		//define('ABSPATH', $mosConfig_absolute_path.'/components/com_mojo/');		
		define('ENABLE_CACHE', true); // cache setting, true means on
		//JDEBUG ? $_MOJO_PROFILER->mark('beginMoJoWPSettings') : null;
		
		require_once(ABSPATH.'wp-settings.php');
		//JDEBUG ? $_MOJO_PROFILER->mark('afterMoJoWPSettings') : null;
		////////////
		/* //original code
		define('DB_NAME', $mosConfig_db);						 
		define('DB_USER', $mosConfig_user);					 
		define('DB_PASSWORD', $mosConfig_password);	 
		define('DB_HOST', $mosConfig_host);			
		// Define table, WP language files and point WP functions to mojo directory.
		$table_prefix  = mosConfig_dbprefix.'wp_';   // only use wp_ as we need this still for plugin support
		define ('WPLANG', '');		
		define('ABSPATH', $mosConfig_absolute_path.'/components/com_mojo/');		
		define('ENABLE_CACHE', true); // cache setting, true means on
		require_once(ABSPATH.'wp-settings.php');
		*/
	// EOF //
	//JDEBUG ? $_MOJO_PROFILER->mark('afterMoJoWPConfig') : null;
	   
//} else {
//	require_once( ABSPATH . 'wp-config.php');
//}

// changed upgrade.php when cache is corrupted, cause... it doesn't upgrade and it confuses folks... kdm
if ( get_option('db_version') != $wp_db_version )
	die (sprintf(__("OOps, mojoBlog needs to refresh some older cached files.  Please <a href='%s'>click to clear your blog cache</a>."), get_option('siteurl') . '/wp-admin/klearkache.php'));
    
require_once(ABSPATH . 'wp-admin/admin-functions.php');
require_once(ABSPATH . 'wp-admin/admin-db.php');

nocache_headers();

update_category_cache();

get_currentuserinfo();

$posts_per_page = get_settings('posts_per_page');
$what_to_show = get_settings('what_to_show');
$date_format = get_settings('date_format');
$time_format = get_settings('time_format');

$wpvarstoreset = array('profile','redirect','redirect_url','a','popuptitle','popupurl','text', 'trackback', 'pingback');
for ($i=0; $i<count($wpvarstoreset); $i += 1) {
    $wpvar = $wpvarstoreset[$i];
    if (!isset($$wpvar)) {
        if (empty($_POST["$wpvar"])) {
            if (empty($_GET["$wpvar"])) {
                $$wpvar = '';
            } else {
                $$wpvar = $_GET["$wpvar"];
            }
        } else {
            $$wpvar = $_POST["$wpvar"];
        }
    }
}

require(ABSPATH . '/wp-admin/menu.php');

// Handle plugin admin pages.
if (isset($_GET['page'])) {
	$plugin_page = stripslashes($_GET['page']);
	$plugin_page = plugin_basename($plugin_page);
	$page_hook = get_plugin_page_hook($plugin_page, $pagenow);

	if ( $page_hook ) {
		if (! isset($_GET['noheader']))
			require_once(ABSPATH . '/wp-admin/admin-header.php');
		
		do_action($page_hook);
	} else {
		if ( validate_file($plugin_page) ) {
			die(__('Invalid plugin page'));
		}
		
		if (! file_exists(ABSPATH . "wp-content/plugins/$plugin_page"))
			die(sprintf(__('Cannot load %s.'), $plugin_page));

		if (! isset($_GET['noheader']))
			require_once(ABSPATH . '/wp-admin/admin-header.php');
		
		include(ABSPATH . "wp-content/plugins/$plugin_page");
	}
	
	include(ABSPATH . 'wp-admin/admin-footer.php');

	exit();
} else if (isset($_GET['import'])) {
	
	$importer = $_GET['import'];

	if ( validate_file($importer) ) {
		die(__('Invalid importer.'));
	}
		
	if (! file_exists(ABSPATH . "wp-admin/import/$importer.php"))
		die(__('Cannot load importer.'));
	
	include(ABSPATH . "wp-admin/import/$importer.php");

	$parent_file = 'import.php';
	$title = __('Import');
	
	if (! isset($_GET['noheader']))
		require_once(ABSPATH . 'wp-admin/admin-header.php');

	require_once(ABSPATH . 'wp-admin/upgrade-functions.php');

	define('WP_IMPORTING', true);
	kses_init_filters();  // Always filter imported data with kses.

	call_user_func($wp_importers[$importer][2]);
			
	include(ABSPATH . 'wp-admin/admin-footer.php');

	exit();
}
/**/
?>
